package com.itheima.dao.impl;

import com.itheima.dao.CategoryDao;
import com.itheima.pojo.Category;
import com.itheima.utils.C3P0Utils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.util.List;

/**
 * @author ZhangYan
 * @Date 2021/1/15
 */
public class CategoryDaoImpl implements CategoryDao {
    //利用C3P0工具类获取DataSource 构建QueryRunner对象 用于还行sql语句
    private QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource());

    @Override
    public List<Category> findAll() throws Exception {

        //sql语句
        String sql = "select * from category";
        //执行sql
        //返回list类型  BeanListHandler<>()
        List<Category> list = queryRunner.query(sql, new BeanListHandler<>(Category.class));
        return list;
    }
}
